package com.cskaoyan.aspect;

import com.cskaoyan.util.TransactionUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.sql.SQLException;

/**
 * @author stone
 * @date 2022/03/23 09:24
 */
@Component
@Aspect
public class TransactionAspect {

    @Pointcut("execution(* com..service..*(..))")
    public void servicePointcut() {
    }

    //使用什么通知 → around

    @Around("servicePointcut()")
    public Object around(ProceedingJoinPoint joinPoint) throws SQLException {
        TransactionUtils.startTransaction();
        Object proceed = null;
        try {
            proceed = joinPoint.proceed();
        } catch (Throwable throwable) {
            TransactionUtils.rollBackTransaction();
            throwable.printStackTrace();
        }
        TransactionUtils.commitTransaction();
        return proceed;

    }
}
